package designPattern.singleton;

public class SingletonTest3 {
    public static void main(String[] args) {
        Singleton3 instance1 = Singleton3.getInstance();
        Singleton3 instance2 = Singleton3.getInstance();

        //判断两个对象是否相同
        System.out.println(instance2==instance1);

        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());
    }
}

//饿汉式（静态常量）

class Singleton3{
    private static Singleton3 instance;

    private Singleton3(){}

    //提供一个静态的公共方法，当调用该方法时，才去创建instance
    //即懒汉式
    public static Singleton3 getInstance(){
        if(instance==null){
            instance = new Singleton3();
        }

        return instance;
    }
}